﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Devices;

namespace TapMe.View
{
    public partial class TutorialPage : PhoneApplicationPage
    {
        private int numFingers;
        private bool hasStarted;
        private bool isActive;

        public TutorialPage()
        {
            InitializeComponent();
            this.hasStarted = false;
            this.numFingers = -1;
            Touch.FrameReported += new TouchFrameEventHandler(HandleTouch);
            this.isActive = false;
        }

        private void HandleTouch(object sender, TouchFrameEventArgs e)
        {
            if (isActive)
            {
                this.numFingers = e.GetTouchPoints(ContentPanel).Count;
                if (hasStarted)
                {
                    InfoTextBlock.Text = string.Format("tap: {0} finger", numFingers.ToString());
                    if (this.numFingers == 1)
                    {
                        ContentPanel.Fill = new SolidColorBrush(Colors.Cyan);
                        ContentPanel.Opacity = 100;
                    }
                    else if (this.numFingers == 2)
                    {
                        ContentPanel.Fill = new SolidColorBrush(Colors.Purple);
                        ContentPanel.Opacity = 100;
                    }
                    else if (this.numFingers == 3)
                    {
                        ContentPanel.Fill = new SolidColorBrush(Colors.Green);
                        ContentPanel.Opacity = 100;
                    }
                }
            }
        }

        private void HandleHold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            InfoTextBlock.Text = "Long Press";
            VibrateController vibrator = VibrateController.Default;
            vibrator.Start(TimeSpan.FromMilliseconds(200));
        }

        private void HandleManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.hasStarted = true;
        }

        private void HandleManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
        }

        private void HandleManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            ContentPanel.Opacity = 0;
            this.numFingers = -1;
            this.hasStarted = false;
            InfoTextBlock.Text = "Tap the screen to learn!";
        }

        #region page navigation handler
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.isActive = true;
            base.OnNavigatedFrom(e);
        }


        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.isActive = false;
            base.OnNavigatedFrom(e);
        }
        #endregion
    }
}